<!DOCTYPE html>
<html>
<head>
<title>Rectangular Waveguide</title>
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #ffffff;
  }
  canvas {
    border: 1px solid #ffffff;
    background-color: #fff;
  }
</style>
</head>
<body>
<canvas id="canvas" width="500" height="450"></canvas>
<script>
  const canvas = document.getElementById('canvas');
  const ctx = canvas.getContext('2d');

  // Style settings
  ctx.lineWidth = 2.5;
  ctx.strokeStyle = 'black';
  ctx.fillStyle = 'black';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'middle';

  // Coordinates
  const originX = 200;
  const originY = 280;
  const b = 160; // width along x
  const a = 90;  // height along y

  const z_dx = -100;
  const z_dy = 60;
  
  const ext_dx = -80;
  const ext_dy = 48;


  // Helper function to draw an arrow
  function drawArrow(fromX, fromY, toX, toY, headLength = 10) {
    const dx = toX - fromX;
    const dy = toY - fromY;
    const angle = Math.atan2(dy, dx);
    ctx.moveTo(fromX, fromY);
    ctx.lineTo(toX, toY);
    ctx.lineTo(toX - headLength * Math.cos(angle - Math.PI / 6), toY - headLength * Math.sin(angle - Math.PI / 6));
    ctx.moveTo(toX, toY);
    ctx.lineTo(toX - headLength * Math.cos(angle + Math.PI / 6), toY - headLength * Math.sin(angle + Math.PI / 6));
  }
  
  // Helper for dimension 'a' arrows
  function drawDimArrow(x, y, up = true, headLength = 8){
      const angle = up ? -Math.PI/2 : Math.PI/2;
      ctx.moveTo(x, y);
      ctx.lineTo(x - headLength * Math.cos(angle - Math.PI / 6), y - headLength * Math.sin(angle - Math.PI / 6));
      ctx.moveTo(x, y);
      ctx.lineTo(x - headLength * Math.cos(angle + Math.PI / 6), y - headLength * Math.sin(angle + Math.PI / 6));
  }


  // --- Drawing Starts Here ---
  ctx.font = 'bold 24px sans-serif';

  // Draw Axes
  ctx.beginPath();
  // Y-axis
  drawArrow(originX, originY + 70, originX, originY - a - 70);
  ctx.fillText('y', originX - 25, originY - a - 75);

  // X-axis
  drawArrow(originX - 30, originY, originX + b + 70, originY);
  ctx.fillText('x', originX + b + 85, originY);

  // Z-axis
  drawArrow(originX, originY, originX + z_dx, originY + z_dy);
  ctx.fillText('z', originX + z_dx - 20, originY + z_dy + 5);
  ctx.stroke();


  // Draw Waveguide
  ctx.beginPath();
  // Front face (rectangle)
  ctx.rect(originX, originY - a, b, a);
  
  // 3D extension lines
  const tl_X = originX;
  const tl_Y = originY - a;
  const tr_X = originX + b;
  const tr_Y = originY - a;
  const br_X = originX + b;
  const br_Y = originY;

  ctx.moveTo(tl_X, tl_Y);
  ctx.lineTo(tl_X + ext_dx, tl_Y + ext_dy);

  ctx.moveTo(tr_X, tr_Y);
  ctx.lineTo(tr_X + ext_dx, tr_Y + ext_dy);

  ctx.moveTo(br_X, br_Y);
  ctx.lineTo(br_X + ext_dx, br_Y + ext_dy);

  ctx.stroke();

  // Draw Dimensions
  // Dimension 'a'
  const dim_a_x = originX + b + 25;
  const dim_a_y_start = originY;
  const dim_a_y_end = originY - a;
  ctx.beginPath();
  ctx.moveTo(dim_a_x, dim_a_y_start);
  ctx.lineTo(dim_a_x, dim_a_y_end);
  drawDimArrow(dim_a_x, dim_a_y_start, false); // Arrow pointing down
  drawDimArrow(dim_a_x, dim_a_y_end, true);   // Arrow pointing up
  ctx.stroke();
  ctx.fillText('a', dim_a_x + 20, originY - a / 2);

  // Dimension 'b'
  const dim_b_y = originY + 30;
  const tick_h = 8;
  const label_b_width = ctx.measureText('b').width;
  const gap = label_b_width + 4;
  
  ctx.beginPath();
  // Ticks
  ctx.moveTo(originX, dim_b_y - tick_h / 2);
  ctx.lineTo(originX, dim_b_y + tick_h / 2);
  ctx.moveTo(originX + b, dim_b_y - tick_h / 2);
  ctx.lineTo(originX + b, dim_b_y + tick_h / 2);
  
  // Horizontal line parts with a gap for the label
  const line_b_start_x = originX;
  const line_b_end_x = originX + b;
  const line_b_mid_x = originX + b / 2;
  ctx.moveTo(line_b_start_x, dim_b_y);
  ctx.lineTo(line_b_mid_x - gap / 2, dim_b_y);
  ctx.moveTo(line_b_mid_x + gap / 2, dim_b_y);
  ctx.lineTo(line_b_end_x, dim_b_y);
  ctx.stroke();
  ctx.fillText('b', line_b_mid_x, dim_b_y);


  // Figure Caption
  ctx.font = '24px sans-serif';
  ctx.fillText('Fig. 4.14', canvas.width / 2, canvas.height - 30);

</script>
</body>
</html>